import router from "@/router"
import type { RouteLocationNormalizedGeneric } from "vue-router"
import { useThemeStore, useTabViewStore } from "@/store"

/**
 * 路由名称
 */
const name = "Redirect"

/**
 * 组装路由
 */
export const buildRefreshRouter = (layout: Component) => {
    return {
        path: "/redirect",
        name,
        component: layout,
        children: [
            {
                path: "/redirect/:path(.*)",
                name,
                component: () => import("./Index.vue")
            }
        ]
    }
}

/**
 * 判断当前页面是否是跳转页面
 */
export const isRefreshRoute = (to: RouteLocationNormalizedGeneric) => {
    return to.name === name
}

/**
 * 执行刷新
 *
 * @param route 当前路由
 */
export const refresh = (route: RouteLocationNormalizedGeneric) => {
    const themeStore = useThemeStore()
    const { useTabView } = storeToRefs(themeStore)
    const tabViewStore = useTabViewStore()
    if (unref(useTabView)) {
        tabViewStore.removeCacheQueue(route)
    }
    router.push({
        path: "/redirect" + unref(route).fullPath
    })
}
